<template>
  <el-container class='layout-container'>
    <el-header class='header'>
      <div class="logo">
        <img src="http://hwss.gitee.io/vue_shop/img/heima.b5a855ee.png"
             alt="">
        <h3>电商管理系统</h3>
      </div>
      <div class='right'>
        <el-dropdown>
          <span class="el-dropdown-link">
            {{username}},您好<i class="el-icon-arrow-down el-icon--right"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item>全屏</el-dropdown-item>
            <el-dropdown-item @click.native='quit'>退出</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </el-header>
    <!-- 底部 -->
    <el-container class='container'>
      <!-- 左边 -->
      <el-aside :width="isCollapse ? '64px': '230px'"
                class='menu'>
        <!-- 切换图标 -->
        <div class="line">
          <i class='el-icon-s-unfold'
             v-if='isCollapse'
             @click='isCollapse=!isCollapse'></i>
          <i class='el-icon-s-fold'
             v-else
             @click='isCollapse=!isCollapse'></i>
        </div>
        <!-- default-active	当前激活菜单的 index -->
        <el-menu router
                 :default-active='$route.path'
                 :collapse-transition='false'
                 background-color="#333744"
                 text-color="#fff"
                 :collapse='isCollapse'
                 active-text-color="#ffd04b">
          <el-submenu :index="item.path+''"
                      v-for='item in menus'
                      :key='item.id'>
            <template slot="title">
              <i :class="['iconfont',fontClassObj['icon-'+item.id]]"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item :index="'/'+itm.path"
                          v-for="itm in item.children"
                          :key='itm.id'>
              <i class="el-icon-setting"></i>
              <span slot="title">{{itm.authName}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右边 -->
      <el-main class='main'>
        <!-- 路由二级出口 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'layout',
  data() {
    const user = JSON.parse(localStorage.getItem('HEIMA_SHOP_USER'))
    return {
      username: user.username,
      isCollapse: false, //不折叠
      menus: [], //菜单数据
      fontClassObj: {
        'icon-125': 'icon-user',
        'icon-103': 'icon-tijikongjian',
        'icon-101': 'icon-shangpin',
        'icon-102': 'icon-danju',
        'icon-145': 'icon-baobiao'
      }
    }
  },
  //生命周期提前先把下面的数据加载出来（同步和异步）
  created() {
    this.getMenu()
  },
  methods: {
    //左侧菜单
    async getMenu() {
      //1.发请求
      const { data: res } = await this.$axios.get('/menus')
      //2.错误处理
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      //3.成功处理
      this.menus = res.data
    },
    //退出功能
    quit() {
      localStorage.removeItem('HEIMA_SHOP_USER')
      this.$router.push('/login')
    }
  }
}
</script>
<style lang='less' >
.layout-container {
  width: 100vw;
  height: 100vh;
  .header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #fff;
    .logo {
      display: flex;
      align-items: center;
      img {
        height: 50px;
      }
      h3 {
        font-size: 20px;
        font-weight: normal;
        margin-left: 10px;
      }
    }
    .el-dropdown-link {
      color: #fff;
    }
  }
  .menu {
    background-color: #333744;
    .line {
      text-align: center;
      color: #fff;
      line-height: 30px;
      background-color: #6a6d79;
      cursor: pointer;
    }
  }
  .el-menu {
    border: none;
  }
  .main {
    background-color: #ddd;
  }
}
</style>
